var FG;
if (!FG)
    FG = {};

// 依赖于 FeatureWidgetManager模块

// Backbone化的FeatureView
FG.LBFeatureView = DX.BBView.extend({
    tagField: 'uuid',
    render: function() {
        var this_ = this,
            app = DX.get('app');
        
        if (!this.model) {
            return this;
        }
    
        var info = FG.parseFeatureModel(this.model);

        if (!_.has(this, 'feature')){
            // TODO: 检测
            this.feature = FG.createFeature(info.featureType, info.latlngs, info.featureOpts);
        } else {
            this.feature.updateFeature(info.latlngs, info.featureOpts);
        }
        if (!this.feature) {
            return this;
        }

        if (!!info.widgetType) {
            if (!_.has(this, 'widget')) {
                this.widget = FG.createWidget(info.widgetType, info.widgetOpts, this.feature);
            }
    
            this.feature.bindPopup(this.widget);
            this.widget.renderModel(this.model);
        }

        this.feature.once('popupopen', function() {
            if (!!this_.widget) {
                this_.widget.renderModel(this_.model);
                // if (!!this_.widget.svView) {
                //     this_.widget.svView.onResize();
                // }
            }
        });

        this.feature.off('click').on('click', function() {
            DX.shout('map:feature:clicked', this_.feature); 
        });

        // TODO: more interactions can be added here.
        DX.shout("feature:rendered", this);
        if (!!this.model.hasData && this.model.hasData()){
            DX.shout("feature:data:rendered", this);
        }
        return this;
    },
    
    setFocus: function(status) {
        if (typeof status === 'undefined' || !!status) {
            if (_.contains(_.functions(this.feature), 'bringToFront'))
                this.feature.bringToFront();
            if (_.contains(_.functions(this.feature), 'setActive'))
                this.feature.setActive();
            if (_.contains(_.functions(this.feature), 'openPopup'))
                this.feature.openPopup();
        } else {
            if (_.contains(_.functions(this.feature), 'bringToBack'))
                this.feature.bringToBack();
            if (_.contains(_.functions(this.feature), 'setInactive'))
                this.feature.setInactive();
            if (_.contains(_.functions(this.feature), 'closePopup'))
                this.feature.closePopup();
        }
    }
});

// Backbone化的FeatureGroupView
FG.LBFeatureGroupView = DX.BBCollectionView.extend({
    modelView: FG.LBFeatureView,
    tagField: 'uuid',
    
    initialize: function() {
        this.entryList = [];
        this.featureGroup = new L.FeatureGroup();
        //this._uuid_feature = {};
    },
    
    addOnMap: function(map) {
        map.addLayer(this.featureGroup);
        return this;
    },
    
    pushEntry: function(entry) {
        if (!entry) {
            return;
        }
        var entryObj = entry;
        var tag = DX.getBBTag(entryObj);
        if (!tag) {
            tag = DX.gen_uuid16();
        }
        var viewEntry = this.getByTag(tag);
        
        var entryExists = (typeof viewEntry !== 'undefined');
        var collapsed;
        try {
            collapsed = entryObj.has('showing_status') && !!entryObj.get('showing_status').collapsed;
        } catch (e) {
            var showing_status;
            if (_.has(entryObj, 'showing_status')) {
                showing_status = entryObj.showing_status;
            } else if (_.has(entryObj, 'properties') && (!!entryObj.properties.showing_status)) {
                showing_status = entryOjb.properties.showing_status;
            }
            
            if (!!showing_status) {
                collapsed = showing_status.collapsed;
            }
        }
        
        if (!!collapsed) {
            if (!!entryExists) {
                this.deleteEntry(viewEntry);
            }
            return;
        }
        
        if (entryExists) {
            viewEntry.model = entryObj;
            viewEntry.render();
        } else {
            viewEntry = new this.modelView({model: entryObj}).render();
            viewEntry.collectionView = this;
            this.entryList.push(viewEntry);
            if (!!viewEntry.feature) {
                this.featureGroup.addLayer(viewEntry.feature);
            }
        }
    },
    
    deleteEntry: function(entry) {
        var this_ = this;
        var delTag = DX.getBBTag(entry);
        if (!delTag) { return; }
        this.entryList = _.reject(this.entryList, function(entry){
            if (entry.getTag() === delTag) {
                if (!!entry.feature) {
                    this_.featureGroup.removeLayer(entry.feature);
                }
                entry.remove();
                return true
            } else {
                return false
            }
        });
    },
    
    clearAll: function(opts) {
        var this_ = this;
        this.featureGroup.clearLayers();
        _.each(this.entryList, function(entry){
            entry.remove();
        });
        this.entryList = [];
    }
});

